package com.jeesite.modules.app.customer.points.details;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.customer.credit.details.entity.TcustomerCreditDetails;
import com.jeesite.modules.zx.customer.points.details.entity.TcustomerPointsDetails;
import com.jeesite.modules.zx.customer.points.details.service.TcustomerPointsDetailsService;

@Controller
@RequestMapping(value = "/app/customer/points/details/")
public class AppCustomerPointsDetailsController {
	@Autowired
	private TcustomerPointsDetailsService tcustomerPointsDetailsService;
	@RequestMapping(value="findPage")
	public void findPage(TcustomerPointsDetails entity ,HttpServletRequest request,HttpServletResponse response){
		entity.setPage(new Page<>(request, response));
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		entity.setCreateBy(userId);
		Page<TcustomerPointsDetails> findPage = tcustomerPointsDetailsService.findPage(entity);
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);		
	}
	@RequestMapping(value="add")
	public void add(TcustomerPointsDetails entity,HttpServletRequest request,HttpServletResponse response){
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		entity.setCreateBy(userId);
		String categoryType = entity.getCategoryType();
		if(StringUtils.isBlank(categoryType)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "类型");
			return;
		}
		String content = entity.getContent();
		if(StringUtils.isBlank(content)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "内容");
			return;
		}
		Long change = entity.getChange();
		if(change==null){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "变动");
			return;
		}
		TcustomerPointsDetails selModel=new TcustomerPointsDetails();
		selModel.setCreateBy(userId);
		List<TcustomerPointsDetails> findList = tcustomerPointsDetailsService.findList(selModel);
		for(TcustomerPointsDetails tcd: findList){
			Long credit = tcd.getPoints();
			credit=credit+change;
			entity.setPoints(credit);
		}
		tcustomerPointsDetailsService.save(entity);
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
}
